package com.one.service.impl;

import com.one.mapper.DogRecordMapper;
import com.one.mapper.OneUserMapper;
import com.one.mapper.StoreMapper;
import com.one.mapper.UserTrasRecordMapper;
import com.one.pojo.*;
import com.one.service.OneUserService;
import com.one.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Description
 * @Author semstouch
 * @Date 2017/4/7
 **/
@Service
public class OneUserServiceImpl implements OneUserService{
    @Resource
    private OneUserMapper oneUserMapper;
    @Resource
    private StoreMapper storeMapper;
    @Resource
    private UserTrasRecordMapper userTrasRecordMapper;
    @Resource
    private DogRecordMapper dogRecordMapper;

    @Override
    public boolean addOneUser(OneUser oneUser) {
        String userId= StringUtils.getUUID();
        oneUser.setUserId(userId);
        oneUser.setFarmId(userId);
        oneUser.setCreateTime(StringUtils.getNowTime());
        Integer rs=oneUserMapper.insertSelective(oneUser);
        if(rs>0){
            Store store = new Store();
            store.setId(userId);
            store.setUserId(userId);
            storeMapper.insertSelective(store);
            return true;
        }
        return  false;
    }

    @Override
    public boolean addOneUserAndStore(OneUser oneUser,Store registerStore,UserTrasRecord userTrasRecord) {
        String userId= StringUtils.getUUID();
        oneUser.setUserId(userId);
        oneUser.setFarmId(userId);
        oneUser.setCreateTime(StringUtils.getNowTime());
        Integer rs=oneUserMapper.insertSelective(oneUser);
        BigDecimal chicksNum=registerStore.getChicksNum();
        chicksNum=chicksNum.subtract(new BigDecimal(330));
        registerStore.setChicksNum(chicksNum);
        if(rs>0){
            Store store = new Store();
            store.setId(userId);
            store.setUserId(userId);
            store.setChicksNum(new BigDecimal(300));

            userTrasRecord.setReceiveId(userId);

            storeMapper.insertSelective(store);
            storeMapper.updateByPrimaryKey(registerStore);
            userTrasRecordMapper.insertSelective(userTrasRecord);
            return true;
        }
        return  false;
    }



    @Override
    public boolean updateOneUser(OneUser oneUser) {
        return oneUserMapper.updateByPrimaryKeySelective(oneUser)>0?true:false;
    }

    @Override
    public boolean updateOneUserAndStore(OneUser oneUser, Store store) {
        oneUserMapper.updateByPrimaryKeySelective(oneUser);
        storeMapper.updateByPrimaryKeySelective(store);
        return true;
    }

    @Override
    public boolean updateOneUserAndStore(OneUser oneUser, Store store, DogRecord dogRecord) {
        oneUserMapper.updateByPrimaryKeySelective(oneUser);
        storeMapper.updateByPrimaryKeySelective(store);
        dogRecordMapper.insertSelective(dogRecord);
        return true;
    }

    @Override
    public boolean userActive(OneUser oneUser, Store store, Store activeStore,UserTrasRecord userTrasRecord) {
        oneUserMapper.updateByPrimaryKeySelective(oneUser);
        storeMapper.updateByPrimaryKeySelective(store);
        storeMapper.updateByPrimaryKeySelective(activeStore);
        userTrasRecordMapper.insertSelective(userTrasRecord);
        return true;
    }

    @Override
    public OneUser findByPhone(String phone) {
        return oneUserMapper.findByPhone(phone);
    }

    @Override
    public OneUser findByLoginName(String loginName) {
        return oneUserMapper.findByLoginName(loginName);
    }

    @Override
    public OneUser findByUserId(String userId) {
        return oneUserMapper.selectByPrimaryKey(userId);
    }

    @Override
    public BigDecimal totalMoney() {
        return oneUserMapper.totalMoney();
    }

    @Override
    public BigDecimal previousMoney(String start, String end) {
        return oneUserMapper.previousMoney(start,end);
    }

    @Override
    public List<OneFriend> findFriend(String upperUserRefId) {
        return oneUserMapper.findFriend(upperUserRefId);
    }

    @Override
    public List<OneUser> findAll() {
        return oneUserMapper.findAll();
    }

    @Override
    public List<OneUser> selectPageList(int offset, int pageSize) {
        return oneUserMapper.selectPageList(offset,pageSize);
    }

    @Override
    public int selectPageCount() {
        return oneUserMapper.selectPageCount();
    }
}
